package com.tanhua.server.controller;

import com.tanhua.dubbo.server.vo.AnswerList;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.dubbo.server.vo.Answer;
import com.tanhua.server.vo.IdentificationVo;
import com.tanhua.server.vo.TestSoulQuestionnaireVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("testSoul")
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /**
     * 控制层实现功能:测灵魂问卷列表
     *
     * @return
     */
    @GetMapping
    public ArrayList<TestSoulQuestionnaireVo> getTestSoulList() {
        return testSoulService.getTestSoulList();
    }


    /**
     * 控制层实现功能_测灵魂:提交灵魂问卷
     *
     * @param answer
     * @return
     */
    @RequestMapping
    public ResponseEntity<String> postTestSoul(@RequestBody AnswerList answer) {


        Answer[] answers = answer.getAnswers().toArray(new Answer[]{});
        try {
            //获取提交问卷的结果
            String result = this.testSoulService.postTestSoul(answers);
            if (result != null) {
                //返回成功提交问卷的结果
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            //提交问卷的结果异常,时返回
            log.error("提交问卷异常", e);
            return ResponseEntity.ok(null);
        }
        //返回失败提交问卷的结果
        return ResponseEntity.ok(null);
    }


    /**
     * 查看报告
     *
     * @param reportId
     * @return
     */
    @GetMapping("report/{id}")
    public ResponseEntity<IdentificationVo> getIdentificationVo(@PathVariable("id") String reportId) {
        IdentificationVo identificationVo = null;
        try {
            //查看测灵魂的最新报告
            identificationVo = testSoulService.getIdentificationVo(reportId);
            //如果没有获取到最新的报告,则返回一个空的报告
            if (identificationVo == null) {
                return ResponseEntity.ok(new IdentificationVo());
            }
        } catch (Exception e) {
            //获取报告失败异常返回时
            log.error("获取报告失败异常!", e);
            return ResponseEntity.ok(new IdentificationVo());
        }
        //返回获取到的最新测灵魂的报告
        return ResponseEntity.ok(identificationVo);

    }

}
